package com.itheima.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.util.BeanHelper;
import com.itheima.common.util.StringUtil;
import com.itheima.sys.dto.IndustryDTO;
import com.itheima.sys.entity.CommonIndustry;
import com.itheima.sys.mapper.CommonIndustryMapper;
import com.itheima.sys.service.ICommonIndustryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 行业字典表 企业-行业字典表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2020-12-13
 */
@Service
public class CommonIndustryServiceImpl extends ServiceImpl<CommonIndustryMapper, CommonIndustry> implements ICommonIndustryService {

    @Override
    public List<IndustryDTO> queryIndustryList(String parentId) {
        QueryWrapper<CommonIndustry> queryWrapper = new QueryWrapper<>();
        //1判断parentID是否有值 有值根据上级ID进行查询子行业
        if(StringUtils.isNotBlank(parentId)){
            queryWrapper.lambda().eq(CommonIndustry::getParentId, parentId);
        }else{
            //2.如果没有提交parentId 查询顶级行业数据
            queryWrapper.lambda().isNull(CommonIndustry::getParentId);
        }
        List<CommonIndustry> commonIndustryList = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(commonIndustryList)){
            return BeanHelper.copyWithCollection(commonIndustryList, IndustryDTO.class);
        }
        return null;
    }
}
